{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.9 \u521b\u5efa\u65b0\u7684\u7c7b\u6216\u5b9e\u4f8b\u5c5e\u6027\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u62e5\u6709\u4e00\u4e9b\u989d\u5916\u529f\u80fd\u7684\u5b9e\u4f8b\u5c5e\u6027\u7c7b\u578b\uff0c\u6bd4\u5982\u7c7b\u578b\u68c0\u67e5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u5b9e\u4f8b\u5c5e\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u63cf\u8ff0\u5668\u7c7b\u7684\u5f62\u5f0f\u6765\u5b9a\u4e49\u5b83\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Descriptor attribute for an integer type-checked attribute\nclass Integer:\n    def __init__(self, name):\n        self.name = name\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            return instance.__dict__[self.name]\n\n    def __set__(self, instance, value):\n        if not isinstance(value, int):\n            raise TypeError('Expected an int')\n        instance.__dict__[self.name] = value\n\n    def __delete__(self, instance):\n        del instance.__dict__[self.name]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u63cf\u8ff0\u5668\u5c31\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u4e09\u4e2a\u6838\u5fc3\u7684\u5c5e\u6027\u8bbf\u95ee\u64cd\u4f5c(get, set, delete)\u7684\u7c7b\uff0c\n\u5206\u522b\u4e3a __get__() \u3001__set__() \u548c __delete__() \u8fd9\u4e09\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\u3002\n\u8fd9\u4e9b\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b9e\u4f8b\u4f5c\u4e3a\u8f93\u5165\uff0c\u4e4b\u540e\u76f8\u5e94\u7684\u64cd\u4f5c\u5b9e\u4f8b\u5e95\u5c42\u7684\u5b57\u5178\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u7528\u4e00\u4e2a\u63cf\u8ff0\u5668\uff0c\u9700\u5c06\u8fd9\u4e2a\u63cf\u8ff0\u5668\u7684\u5b9e\u4f8b\u4f5c\u4e3a\u7c7b\u5c5e\u6027\u653e\u5230\u4e00\u4e2a\u7c7b\u7684\u5b9a\u4e49\u4e2d\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point:\n    x = Integer('x')\n    y = Integer('y')\n\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u8fd9\u6837\u505a\u540e\uff0c\u6240\u6709\u5bf9\u63cf\u8ff0\u5668\u5c5e\u6027(\u6bd4\u5982x\u6216y)\u7684\u8bbf\u95ee\u4f1a\u88ab\n__get__() \u3001__set__() \u548c __delete__() \u65b9\u6cd5\u6355\u83b7\u5230\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Point(2, 3)\np.x # Calls Point.x.__get__(p,Point)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p.y = 5 # Calls Point.y.__set__(p, 5)\np.x = 2.3 # Calls Point.x.__set__(p, 2.3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u8f93\u5165\uff0c\u63cf\u8ff0\u5668\u7684\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u4f1a\u63a5\u53d7\u4e00\u4e2a\u64cd\u4f5c\u5b9e\u4f8b\u3002\n\u4e3a\u4e86\u5b9e\u73b0\u8bf7\u6c42\u64cd\u4f5c\uff0c\u4f1a\u76f8\u5e94\u7684\u64cd\u4f5c\u5b9e\u4f8b\u5e95\u5c42\u7684\u5b57\u5178(__dict__\u5c5e\u6027)\u3002\n\u63cf\u8ff0\u5668\u7684 self.name \u5c5e\u6027\u5b58\u50a8\u4e86\u5728\u5b9e\u4f8b\u5b57\u5178\u4e2d\u88ab\u5b9e\u9645\u4f7f\u7528\u5230\u7684key\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u63cf\u8ff0\u5668\u53ef\u5b9e\u73b0\u5927\u90e8\u5206Python\u7c7b\u7279\u6027\u4e2d\u7684\u5e95\u5c42\u9b54\u6cd5\uff0c\n\u5305\u62ec @classmethod \u3001@staticmethod \u3001@property \uff0c\u751a\u81f3\u662f __slots__ \u7279\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u63cf\u8ff0\u5668\uff0c\u4f60\u53ef\u4ee5\u5728\u5e95\u5c42\u6355\u83b7\u6838\u5fc3\u7684\u5b9e\u4f8b\u64cd\u4f5c(get, set, delete)\uff0c\u5e76\u4e14\u53ef\u5b8c\u5168\u81ea\u5b9a\u4e49\u5b83\u4eec\u7684\u884c\u4e3a\u3002\n\u8fd9\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u6709\u4e86\u5b83\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5f88\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5e76\u4e14\u5b83\u4e5f\u662f\u5f88\u591a\u9ad8\u7ea7\u5e93\u548c\u6846\u67b6\u4e2d\u7684\u91cd\u8981\u5de5\u5177\u4e4b\u4e00\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u63cf\u8ff0\u5668\u7684\u4e00\u4e2a\u6bd4\u8f83\u56f0\u60d1\u7684\u5730\u65b9\u662f\u5b83\u53ea\u80fd\u5728\u7c7b\u7ea7\u522b\u88ab\u5b9a\u4e49\uff0c\u800c\u4e0d\u80fd\u4e3a\u6bcf\u4e2a\u5b9e\u4f8b\u5355\u72ec\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u65e0\u6cd5\u5de5\u4f5c\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Does NOT work\nclass Point:\n    def __init__(self, x, y):\n        self.x = Integer('x') # No! Must be a class variable\n        self.y = Integer('y')\n        self.x = x\n        self.y = y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u65f6\uff0c__get__() \u65b9\u6cd5\u5b9e\u73b0\u8d77\u6765\u6bd4\u770b\u4e0a\u53bb\u8981\u590d\u6742\u5f97\u591a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Descriptor attribute for an integer type-checked attribute\nclass Integer:\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            return instance.__dict__[self.name]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__get__() \u770b\u4e0a\u53bb\u6709\u70b9\u590d\u6742\u7684\u539f\u56e0\u5f52\u7ed3\u4e8e\u5b9e\u4f8b\u53d8\u91cf\u548c\u7c7b\u53d8\u91cf\u7684\u4e0d\u540c\u3002\n\u5982\u679c\u4e00\u4e2a\u63cf\u8ff0\u5668\u88ab\u5f53\u505a\u4e00\u4e2a\u7c7b\u53d8\u91cf\u6765\u8bbf\u95ee\uff0c\u90a3\u4e48 instance \u53c2\u6570\u88ab\u8bbe\u7f6e\u6210 None \u3002\n\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6807\u51c6\u505a\u6cd5\u5c31\u662f\u7b80\u5355\u7684\u8fd4\u56de\u8fd9\u4e2a\u63cf\u8ff0\u5668\u672c\u8eab\u5373\u53ef(\u5c3d\u7ba1\u4f60\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u5176\u4ed6\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c)\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Point(2,3)\np.x # Calls Point.x.__get__(p, Point)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Point.x # Calls Point.x.__get__(None, Point)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u63cf\u8ff0\u5668\u901a\u5e38\u662f\u90a3\u4e9b\u4f7f\u7528\u5230\u88c5\u9970\u5668\u6216\u5143\u7c7b\u7684\u5927\u578b\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u7ec4\u4ef6\u3002\u540c\u65f6\u5b83\u4eec\u7684\u4f7f\u7528\u4e5f\u88ab\u9690\u85cf\u5728\u540e\u9762\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u57fa\u4e8e\u63cf\u8ff0\u5668\u7684\u4ee3\u7801\uff0c\u5e76\u6d89\u53ca\u5230\u4e00\u4e2a\u7c7b\u88c5\u9970\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Descriptor for a type-checked attribute\nclass Typed:\n    def __init__(self, name, expected_type):\n        self.name = name\n        self.expected_type = expected_type\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            return instance.__dict__[self.name]\n\n    def __set__(self, instance, value):\n        if not isinstance(value, self.expected_type):\n            raise TypeError('Expected ' + str(self.expected_type))\n        instance.__dict__[self.name] = value\n    def __delete__(self, instance):\n        del instance.__dict__[self.name]\n\n# Class decorator that applies it to selected attributes\ndef typeassert(**kwargs):\n    def decorate(cls):\n        for name, expected_type in kwargs.items():\n            # Attach a Typed descriptor to the class\n            setattr(cls, name, Typed(name, expected_type))\n        return cls\n    return decorate\n\n# Example use\n@typeassert(name=str, shares=int, price=float)\nclass Stock:\n    def __init__(self, name, shares, price):\n        self.name = name\n        self.shares = shares\n        self.price = price"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8981\u6307\u51fa\u7684\u4e00\u70b9\u662f\uff0c\u5982\u679c\u4f60\u53ea\u662f\u60f3\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u67d0\u4e2a\u7c7b\u7684\u5355\u4e2a\u5c5e\u6027\u8bbf\u95ee\u7684\u8bdd\u5c31\u4e0d\u7528\u53bb\u5199\u63cf\u8ff0\u5668\u4e86\u3002\n\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f7f\u75288.6\u5c0f\u8282\u4ecb\u7ecd\u7684property\u6280\u672f\u4f1a\u66f4\u52a0\u5bb9\u6613\u3002\n\u5f53\u7a0b\u5e8f\u4e2d\u6709\u5f88\u591a\u91cd\u590d\u4ee3\u7801\u7684\u65f6\u5019\u63cf\u8ff0\u5668\u5c31\u5f88\u6709\u7528\u4e86\n(\u6bd4\u5982\u4f60\u60f3\u5728\u4f60\u4ee3\u7801\u7684\u5f88\u591a\u5730\u65b9\u4f7f\u7528\u63cf\u8ff0\u5668\u63d0\u4f9b\u7684\u529f\u80fd\u6216\u8005\u5c06\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5e93\u7279\u6027)\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}